package com.bluesky.objectworld;

import com.bluesky.objectworld.method.block.Block;
import com.bluesky.objectworld.method.block.SequentialBlock;
import com.bluesky.objectworld.primitive.PrimitiveType;

public class VirtualMachine {

	public World world;
	public _Object console;

	/**
	 * Send a message, from start.
	 * 
	 * @param receiver
	 * @param sender
	 *            null-able
	 * @param messageName
	 * @param messageParameters
	 *            null-able
	 */
	static void SEND_MESSAGE(_Object receiver, _Object sender,
			String messageName, _Object... messageParameters) {

		receiver.processMessage(messageName, messageParameters);

	}

	public static VirtualMachine instance;

	public static VirtualMachine getInstance() {
		if (instance == null) {
			instance = new VirtualMachine();
		}
		return instance;
	}

	public VirtualMachine() {
		// first object
		world = new World();
		world.id = 0;

		// create console

		console = world.processMessage("layEgg", world.create(
				PrimitiveType.String, "com.bluesky.objectworld.io.Console"));
	}

	private void test() {
		for (int i = 0; i < 3; i++) {
			// born a new egg
			_Object egg = world.processMessage("layEgg");
			System.out.println(egg);
		}

		_Object a, b, c;
		a = world.create(PrimitiveType.Integer, "3");
		System.out.println("a:" + a);
		b = world.create(PrimitiveType.Integer, "7");
		System.out.println("b:" + b);
		c = a.processMessage("mul", b);
		System.out.println("c:" + c);

		SEND_MESSAGE(console, null, "print", c);
		SEND_MESSAGE(console, null, "newLine");
		SEND_MESSAGE(console, null, "print", c);

	}

	public static void main(String[] args) {
		getInstance().test();
	}
}
